package other;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

// 颜色渐变(径向渐变-RadialGradient,即从内到外进行圆形渐变;线性渐变-LinearGradient;还可以找别人做好的渐变图)
public class GradientDemo extends Application {
	private HBox root = new HBox(30);
	private Rectangle r1;
	private Rectangle r2;
	private Rectangle r3;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		root.setAlignment(Pos.CENTER);
//		root.setStyle("-fx-background-color: linear-gradient(to bottom right, #63B8FF, #9400D3)");	// 方向加颜色
		
		// 线性渐变(true为坐标与形状大小成比例,从0,0到1,1即从左上角到右下角,循环方式,然后设置渐变颜色,方向上颜色开始的位置占总长度的比例)
		r1 = new Rectangle(200, 200);
		LinearGradient lg = new LinearGradient(0, 0, 0.5, 0, true, CycleMethod.REFLECT, 	// 反射填充剩余的
				new Stop(0, Color.RED), new Stop(0.5, Color.CORNFLOWERBLUE), new Stop(1, Color.YELLOW)); 
		r1.setFill(lg);
		
		r2 = new Rectangle(150, 250);
		r2.setStyle("-fx-fill: linear-gradient(from 0px 0px to 0px 100px, yellow, green)");		// 黄色的范围为0-100
		
		// 径向渐变(角度,距离(圆心向指定角度偏移指定比例的环宽度的距离),圆心坐标,循环的环的宽度,循环方式,然后设置渐变颜色)
		r3 = new Rectangle(200, 200);
		RadialGradient rg = new RadialGradient(0, 0, 100, 100, 20, false, CycleMethod.REFLECT, 
				new Stop(0, Color.RED), new Stop(0.5, Color.CORNFLOWERBLUE), new Stop(1, Color.YELLOW));
		r3.setFill(rg);
		root.getChildren().addAll(r1, r2, r3);
		
		Scene scene = new Scene(root, 800, 400);
		stage.setScene(scene);
		stage.show();
	}
}
